package com.xbongbong.saas.model.impl;

import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.enums.DelEnum;
import com.xbongbong.paas.enums.EnableEnum;
import com.xbongbong.paas.enums.StageTypeEnum;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.CollectionsUtil;
import com.xbongbong.paas.toolbox.util.DateUtil;
import com.xbongbong.saas.constant.BasicConstant;
import com.xbongbong.saas.constant.StringConstant;
import com.xbongbong.saas.domain.dao.SaleStageDao;
import com.xbongbong.saas.domain.entity.SaleStageEntity;
import com.xbongbong.saas.model.SaleStageModel;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author haibin.zhang
 * @version v1.0
 * @date 2019/2/14 12:19
 * @since v1.0
 */
@Service
public class SaleStageModelImpl implements SaleStageModel {

    @Resource
    private SaleStageDao saleStageDao;

    @Override
    public Integer insert(SaleStageEntity entity) throws XbbException {
        entity.setAddTime((long) DateUtil.getInt());
        entity.setUpdateTime((long) DateUtil.getInt());
        saleStageDao.insert(entity);
        return entity.getId().intValue();
    }

    @Override
    public Integer update(SaleStageEntity entity) throws XbbException {
        entity.setUpdateTime((long) DateUtil.getInt());
        saleStageDao.update(entity);
        return entity.getId().intValue();
    }

    @Override
    public List<SaleStageEntity> findEntitys(Map<String, Object> param) {
        return saleStageDao.findEntitys(param);
    }

    @Override
    public Integer getEntitysCount(Map<String, Object> param) {
        return saleStageDao.getEntitysCount(param);
    }

    @Override
    public List findEntitysWithoutSub(Map<String, Object> param) {
        return null;
    }

    @Override
    public SaleStageEntity getByKey(Long key, String corpid) {
        return saleStageDao.getByKey(key,corpid);
    }

    @Override
    public SaleStageEntity getByCode(String corpid, Long formId, String code) {
        return saleStageDao.getByCode(corpid, formId, code);
    }

    @Override
    public List<SaleStageEntity> getByCodeIn(String corpid, Long formId, Collection<String> codeIn) {
        if (CollectionUtils.isEmpty(codeIn)) {
            return new ArrayList<>();
        }
        Map<String,Object> map = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        map.put("corpid", corpid);
        map.put("del",0);
        map.put("formId", formId);
        map.put("codeIn", codeIn);
        return findEntitys(map);
    }

    @Override
    public Long save(SaleStageEntity saleStageEntity) throws XbbException {
        if (saleStageEntity.getId() != null){
            update(saleStageEntity);
        }else {
            insert(saleStageEntity);
        }
        return saleStageEntity.getId();
    }

    @Override
    public List<SaleStageEntity> getDefaultList() {
        Map<String,Object> map = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        map.put("corpid","0");
        map.put("del",0);
        return saleStageDao.findEntitys(map);
    }

    @Override
    public void insertBatch(List<SaleStageEntity> list) {
        saleStageDao.insertBatch(list);
    }

    @Override
    public SaleStageEntity getFirstSuccessStage(String corpid, Long formId, Long stageProcessId) {
        return saleStageDao.getFirstSuccessStage(corpid, formId, stageProcessId);
    }

    @Override
    public SaleStageEntity getFirstStage(String corpid, Long formId, Long stageProcessId) {
        return saleStageDao.getFirstStage(corpid, formId, stageProcessId);
    }

    @Override
    public List<SaleStageEntity> getList(String corpid, Long formId, Integer enable, Integer type) {
        Map<String,Object> map = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        map.put("corpid", corpid);
        if (Objects.nonNull(formId)) {
            map.put("formId", formId);
        }
        if (Objects.nonNull(enable)) {
            map.put("enable", enable);
        }
        if (Objects.nonNull(type)) {
            map.put("type", type);
        }
        map.put("del", 0);
        map.put("orderByStr", "sort desc");
        return findEntitys(map);
    }

    @Override
    public Map<String, String> getAllStageMap(String corpid) {
        List<SaleStageEntity> list = getList(corpid, null, null, null);
        Map<String,String> allStageMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        for (SaleStageEntity entity : list){
            String key = entity.getFormId() + "_" + entity.getCode();
            allStageMap.put(key,entity.getName());
        }
        return allStageMap;
    }

    @Override
    public void updateBatch(List<SaleStageEntity> saleStageEntityList, String corpid) {
        saleStageDao.updateBatch(saleStageEntityList, corpid);
    }

    @Override
    public void updateBatchDefaultAndLinkOption(List<SaleStageEntity> saleStageEntityList, String corpid) {
        saleStageDao.updateBatchDefaultAndLinkOption(saleStageEntityList, corpid);
    }

    @Override
    public void updateBatch4MirrorRollback(List<SaleStageEntity> saleStageEntityList, String corpid) {
        saleStageDao.updateBatch4MirrorRollback(saleStageEntityList, corpid);
    }

    @Override
    public void updateBatch4Script(List<SaleStageEntity> saleStageEntityList) {
        if(CollectionsUtil.isNotEmpty(saleStageEntityList)) {
            int size = saleStageEntityList.size();
            int page = (size - 1) / 1000;
            for (int currPage = 0; currPage <= page; currPage++) {
                int offset = currPage * 1000;
                saleStageDao.updateBatch4Script(saleStageEntityList.subList(offset, (size - offset < 1000) ? size : offset + 1000));
            }
        }
    }

    @Override
    public Integer insert4Script(SaleStageEntity entity) throws XbbException {
        return saleStageDao.insert4Script(entity);

    }

    @Override
    public void insertBatch4Script(List<SaleStageEntity> list) {
        saleStageDao.insertBatch4Script(list);
    }

    @Override
    public Integer deleteBatch(List<SaleStageEntity> stageEntityList, String corpid) {
        return saleStageDao.deleteBatch(stageEntityList, corpid);
    }

    @Override
    public SaleStageEntity getFirstCreateContractSkipSuccessStage(String corpid, Long formId, Long stageProcessId) {
        Map<String, Object> param = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        param.put(StringConstant.CORPID, corpid);
        param.put("formId", formId);
        param.put("stageProcessId", stageProcessId);
        param.put("type", StageTypeEnum.SUCCESS_STAGE.getType());
        param.put(StringConstant.DEL, DelEnum.NORMAL.getDel());
        param.put("enable", EnableEnum.OPEN.getCode());
        param.put("allowCreateContractSkipStage", EnableEnum.OPEN.getCode());
        param.put(StringConstant.ORDER_BY, "sort desc");
        param.put(StringConstant.START, BasicConstant.ZERO);
        param.put("pageNum", BasicConstant.ONE);
        List<SaleStageEntity> entitys = findEntitys(param);
        return CollectionsUtil.isNotEmpty(entitys) ? entitys.get(BasicConstant.ZERO) : null;
    }
}
